extends VBoxContainer


@onready var shield_bar: BufferProgressBar = $HBoxContainer/ShieldBar
@onready var shield_lbl: Label = $HBoxContainer/ShieldLbl
@onready var health_bar: BufferProgressBar = $HBoxContainer2/HealthBar
@onready var health_icon: TextureRect = $HBoxContainer2/HealthIcon
@onready var health_lbl: Label = $HBoxContainer2/HealthLbl
@onready var exp_bar: BufferProgressBar = $HBoxContainer3/ExpBar
@onready var lvl_lbl: Label = $HBoxContainer3/LvlLbl

@onready var _player: Player = get_tree().get_first_node_in_group("player")


func _ready() -> void:
    if _player == null: return

    # 数据绑定
    _bind_player_data_to_buff_progress_bar("shield", shield_bar, shield_lbl)
    _bind_player_data_to_buff_progress_bar("health", health_bar, health_lbl)

    _bind_player_experience_to_experience_bar()

    # 初始化数据
    _init_buff_progress_bar("shield", shield_bar, shield_lbl)
    _init_buff_progress_bar("health", health_bar, health_lbl)
    _init_experience_bar()


    # TODO 添加动态动画效果
    _create_heart_beating_tween()
    

func _bind_player_data_to_buff_progress_bar(data_property: StringName, bar: BufferProgressBar, lbl: Label) -> void:
    var item := _player.data.get(data_property) as DataItem
    
    item.max_value_changed.connect(
        func(_ov: float, nv: float) -> void:
            bar.max_value = nv
    )

    item.min_value_changed.connect(
        func(_ov: float, nv: float) -> void:
            bar.min_value = nv
    )

    item.value_changed.connect(
        func(_ov: float, nv: float) -> void:
            bar.value = nv
            lbl.text = str(int(nv))
    )

func _bind_player_experience_to_experience_bar() -> void:
    _player.data.level_changed.connect(
        func(_ov: int, nv: int) -> void:
            exp_bar.max_value = _player.data.calc_total_experience_from_level(nv)
            exp_bar.value = _player.data.total_experience.value
            _update_lvl_lbl(nv)
            _create_upgrade_tween()
    )


func _init_buff_progress_bar(data_property: StringName, bar: BufferProgressBar, lbl: Label) -> void:
    var item := _player.data.get(data_property) as DataItem
    bar.max_value = item.max_value
    bar.min_value = item.min_value
    bar.value = item.value
    lbl.text = str(int(item.value))


func _init_experience_bar() -> void:
    exp_bar.max_value = _player.data.calc_total_experience_from_level(1)
    exp_bar.min_value = 0
    exp_bar.value = 0


func _update_lvl_lbl(lvl: int) -> void:
    lvl_lbl.text = "lv." + str(lvl)


func _create_heart_beating_tween() -> void:
    # health_icon.pivot_offset = health_icon.size / 2

    var tween := create_tween().bind_node(self).set_trans(Tween.TRANS_CUBIC)
    tween.tween_property(health_icon, "scale", Vector2(1.2, 1.2), 0.75)
    tween.tween_property(health_icon, "scale", Vector2(1, 1), 0.75)
    tween.set_loops().play()


func _create_upgrade_tween() -> void:
    var tween := create_tween().bind_node(self).set_trans(Tween.TRANS_CUBIC)
    tween.tween_property(lvl_lbl, "scale", Vector2(1.5, 1.5), 0.25)
    tween.tween_property(lvl_lbl, "scale", Vector2(1, 1), 0.15)
    tween.play()
